#include "common/pch.h"
#include "map/terrainmgr.h"

TerrainManager::TerrainManager()
{
}

TerrainManager::~TerrainManager()
{
    for (TerrainDataMap::iterator it = i_TerrainMap.begin(); it != i_TerrainMap.end(); ++it)
    {
        delete it->second;
    }
}

TerrainInfo* TerrainManager::LoadTerrain(const unsigned int mapId)
{

        TerrainDataMap::const_iterator iter = i_TerrainMap.find(mapId);
    if (iter == i_TerrainMap.end())
    {
        TerrainInfo* ti = new TerrainInfo(mapId);
        i_TerrainMap[mapId] = ti;
        return ti;
    }
    return (*iter).second;
}

void TerrainManager::UnloadTerrain(const unsigned int mapId)
{
#if 0
    if (sWorld.getConfig(CONFIG_BOOL_GRID_UNLOAD) == 0)
    {
        return;
    }

    ACE_GUARD(LOCK_TYPE, _guard, m_mutex)

        TerrainDataMap::iterator iter = i_TerrainMap.find(mapId);
    if (iter != i_TerrainMap.end())
    {
        TerrainInfo* ptr = (*iter).second;
        // lets check if this object can be actually freed
        if (ptr->IsReferenced() == false)
        {
            i_TerrainMap.erase(iter);
            delete ptr;
        }
    }
#endif
}

void TerrainManager::Update(const unsigned int diff)
{
    // global garbage collection for GridMap objects and VMaps
    for (TerrainDataMap::iterator iter = i_TerrainMap.begin(); iter != i_TerrainMap.end(); ++iter)
    {
        iter->second->CleanUpGrids(diff);
    }
}

void TerrainManager::UnloadAll()
{
    for (TerrainDataMap::iterator it = i_TerrainMap.begin(); it != i_TerrainMap.end(); ++it)
    {
        delete it->second;
    }

    i_TerrainMap.clear();
}

unsigned int TerrainManager::GetAreaIdByAreaFlag(unsigned short areaflag, unsigned int map_id)
{
#if 1
    return 0;
#else
    AreaTableEntry const* entry = GetAreaEntryByAreaFlagAndMap(areaflag, map_id);

    if (entry)
    {
        return entry->ID;
    }
    else
    {
        return 0;
    }
#endif
}

unsigned int TerrainManager::GetZoneIdByAreaFlag(unsigned short areaflag, unsigned int map_id)
{
#if 1
    return 0;
#else;
    AreaTableEntry const* entry = GetAreaEntryByAreaFlagAndMap(areaflag, map_id);

    if (entry)
    {
        return (entry->zone != 0) ? entry->zone : entry->ID;
    }
    else
    {
        return 0;
    }
#endif
}

void TerrainManager::GetZoneAndAreaIdByAreaFlag(unsigned int& zoneid, unsigned int& areaid, unsigned short areaflag, unsigned int map_id)
{
#if 0
    AreaTableEntry const* entry = GetAreaEntryByAreaFlagAndMap(areaflag, map_id);

    areaid = entry ? entry->ID : 0;
    zoneid = entry ? ((entry->zone != 0) ? entry->zone : entry->ID) : 0;
#endif
}
